package cn.lyq.card.manager

import androidx.lifecycle.MutableLiveData
import cn.lyq.card.data.model.AddressInfo
import cn.lyq.card.data.model.BankCardInfo
import cn.lyq.card.data.model.BillTypeInfo
import cn.lyq.card.data.model.UserInfo
import cn.lyq.card.global.Const
import cn.lyq.card.global.SPKeyGlobal
import com.blankj.utilcode.util.SPUtils


object UserManager {

    val instance = MutableLiveData<UserInfo>()

    val unreadNotice = MutableLiveData<Int>()

    var addressInfo: AddressInfo? = null

    var bankCardInfo: BankCardInfo? = null

    var billTypeList = mutableMapOf<Int, BillTypeInfo>()

    var phoneStr: String? = null

    var customer: String? = null

    fun post(userInfo: UserInfo?) {
        instance.postValue(userInfo)
    }

    fun setPhone(phone: String) {
        SPUtils.getInstance().put(SPKeyGlobal.APP.PHONE, phone)
    }

    fun getPhone(): String {
        if (phoneStr == null || phoneStr?.isEmpty() != false) {
            phoneStr = SPUtils.getInstance().getString(SPKeyGlobal.APP.PHONE, "")
        }
        return phoneStr ?: ""
    }

    fun setDefaultAddress(data: MutableList<AddressInfo>) {
        data.forEach { info ->
            if (info.isDefault()) {
                addressInfo = info
                return
            }
        }
        addressInfo = data.takeIf { it.size > 0 }?.run { get(0) }
    }
}